/**
 * Package name:com.feiniu
 * File name:FeiniuLoginMain.java
 * Date:2016年8月3日-上午9:20:49
 * feiniu.com Inc.Copyright (c) 2013-2015 All Rights Reserved.
 *
 */
package com.feiniu;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import indi.yangshenhui.tool.FileUtil;
import indi.yangshenhui.tool.FileUtil.ReaderHandler;
import indi.yangshenhui.tool.dao.DBHelper;

/**
 * @ClassName FeiniuLoginMain
 * @Description
 * @date 2016年8月3日 上午9:20:49
 * @author shenhui.yang
 * @version 1.0.0
 *
 */
public class FeiniuLoginMain {
	public static final String filePath = "C:/Users/shenhui.yang/Downloads/login/20160801_login.txt";

	public static void main(String[] args) throws IOException {
		LoginLogFileReader loginLogFileReader = new FeiniuLoginMain().new LoginLogFileReader();
		int result = FileUtil.loadDataFromfile(loginLogFileReader, filePath, null);
		System.out.println("result:" + result);
	}

	public class LoginLogFileReader implements ReaderHandler<Integer> {

		@Override
		public Integer handle(BufferedReader reader) throws IOException {
			String sql = "INSERT INTO feiniu_login_log(server_name,login_time,request_ip,api_version,app_version,area_code,channel,city_code,client_id,device_id,token,view_size,username) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			String serverName = "";
			String loginTime = "";
			String requestIp = "";
			String apiVersion = "";
			String appVersion = "";
			String areaCode = "";
			String channel = "";
			String cityCode = "";
			String clientId = "";
			String deviceId = "";
			String token = "";
			String viewSize = "";
			String username = "";
			JSONObject data = null;

			String line = reader.readLine();
			int count = 0;
			int result = 0;
			long start = System.currentTimeMillis();
			long end = 0;
			while (line != null) {
				++count;
				try {
					serverName = line.substring(0, line.indexOf("|"));
					loginTime = line.substring(line.indexOf("[") + 1, line.indexOf("[") + 20);
					requestIp = getRequestIp(line);
					data = JSON.parseObject(line.substring(line.indexOf("=login:") + 7, line.length()));
					apiVersion = data.getString("apiVersion");
					appVersion = data.getString("appVersion");
					areaCode = data.getString("areaCode");
					channel = data.getString("channel");
					cityCode = data.getString("cityCode");
					clientId = data.getString("clientid");
					deviceId = data.getString("device_id");
					token = data.getString("token");
					viewSize = data.getString("view_size");
					username = data.getJSONObject("body").getString("username");
					result = DBHelper.executeNonQuery(sql, serverName, loginTime, requestIp, apiVersion, appVersion,
							areaCode, channel, cityCode, clientId, deviceId, token, viewSize, username);
					if (result == 0) {
						DBHelper.executeNonQuery("INSERT INTO feiniu_login_log_error_line(line) VALUES(?)", line);
					}
				} catch (Exception e) {
					DBHelper.executeNonQuery("INSERT INTO feiniu_login_log_error_line(line) VALUES(?)", line);
					System.err.println("error line:" + line);
					e.printStackTrace();
				}
				line = reader.readLine();
				if (count % 10 == 0) {
					end = System.currentTimeMillis();
					System.err.println("count:" + count+" ---cost:"+(end-start));
					start = System.currentTimeMillis();
				}
			}
			return 1;
		}

		private String getRequestIp(String s) {
			String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(s);
			while (m.find()) {
				return m.group();
			}
			return "";
		}

	}

}
